<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class ChannelQr extends BaseModel
{
    //定义表名
    protected $name = 'massage_channel_qr';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2023-08-01 10:00
     * @功能说明:列表
     */
    public function qrDataList($dis,$page=10){

        $data = $this->alias('a')
            ->join('massage_channel_list b','a.channel_id = b.id AND b.status=2','left')
            ->where($dis)
            ->field('a.*,b.user_name as channel_name,b.user_id')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        if(!empty($data['data'])){

            $cate_model = new ChannelCate();

            $user_model = new User();

            foreach ($data['data'] as &$v){

                $v['nickName'] = $user_model->where(['id'=>$v['user_id']])->value('nickName');

                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');
            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2023-07-25 18:21
     * @功能说明:后端列表
     */
    public function adminDataList($dis,$dis_sql,$type,$page=1,$limit=10){

        switch ($type){

            case 1:
                $sort = 'scan_qr_count';
                break;
            case 2:
                $sort = 'order_count';
                break;
            case 3:
                $sort = 'order_price';
                break;
            case 4:
                $sort = 'channel_cash';
                break;
            default:
                $sort = 'id';
                break;
        }

        $count = $this->alias('a')
                ->join('massage_channel_list b','a.channel_id = b.id','left')
                ->where($dis)
                ->group('a.id')
                ->count();

        $start = $limit;

        $end   = ($page-1)*$limit;

        $sql = "SELECT a.*,d.scan_qr_count,e.order_price,e.order_count,f.channel_cash,b.user_name as channel_name,b.user_id
             FROM `ims_massage_channel_qr` `a`
             LEFT JOIN `ims_massage_channel_list` `b` ON a.channel_id=b.id AND b.status = 2
             LEFT JOIN (SELECT count(id) as scan_qr_count,qr_id FROM `ims_massage_channel_scan_code_record` GROUP BY qr_id) AS d ON a.id=d.qr_id 
             LEFT JOIN (SELECT sum(true_service_price) as order_price,count(id) as order_count,channel_qr_id,id FROM `ims_massage_service_order_list` where pay_type = 7 GROUP BY channel_qr_id) AS e ON a.id=e.channel_qr_id 
             LEFT JOIN (SELECT sum(cash) as channel_cash,channel_qr_id FROM `ims_massage_service_order_commission` where status = 2 GROUP BY channel_qr_id) AS f ON a.id=f.channel_qr_id
             WHERE $dis_sql
             GROUP BY a.id ORDER BY $sort DESC LIMIT $start OFFSET $end";

        $data = Db::query($sql);

        if(!empty($data)){

            $cate_model = new ChannelCate();

            $user_model = new User();

            foreach ($data as &$v){

                $v['nickName'] = $user_model->where(['id'=>$v['user_id']])->value('nickName');
                $v['scan_qr_count'] = !empty($v['scan_qr_count'])?$v['scan_qr_count']:0;
                $v['order_price']  = !empty($v['order_price'])?round($v['order_price'],2):0;
                $v['order_count']  = !empty($v['order_count'])?$v['order_count']:0;
                $v['channel_cash'] = !empty($v['channel_cash'])?round($v['channel_cash'],2):0;
                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');
            }
        }

        $arr['data'] = $data;

        $arr['total']= $count;

        $arr['current_page'] = $page;

        $arr['per_page']     = $limit;

        $arr['last_page']    = ceil($arr['total']/$limit);

        return $arr;
    }



    /**
     * @author chenniang
     * @DataTime: 2023-07-25 15:11
     * @功能说明:二维码画图将编号加上
     */
    public function channelQrImg($data){

        $im    = imagecreatetruecolor(460, 500);

        $color = imagecolorallocate($im, 255, 255, 255);

        imagefill($im, 0, 0, $color);

        $font_file = APP_PATH . "Common/extend/vista.ttf";

        list( $l_w, $l_h ) = getimagesize( $data[ 'qr' ] );

        $ext = longbingSingleGetImageExtWx( $data[ 'qr' ] );

        if ( $ext == 'jpg' || $ext == 'jpeg' )
        {
            $logoImg = @imagecreatefromjpeg( $data[ 'qr' ] );
        }
        else if ( $ext == 'png' )
        {
            $logoImg = @imagecreatefrompng( $data[ 'qr' ] );
        }
        else
        {
            return false;
        }
        imagecopyresized($im, $logoImg, 0, 0, 0, 0, 450, 450, $l_w, $l_h);

        $black = imagecolorallocate($logoImg, 0, 0, 0);

        imagettftext($im, 25, 0, 100, 470, $black, $font_file, $data['code']); //现价

        $path = MATER_UPLOAD_PATH.date('Y-m-d',time()).'/img';

        if(!is_dir($path)){

            mkdir($path,0777,true);
        }

        $imageName = $path."/25220_".date("His",time())."_".rand(1111,9999).'.jpg';

        imagepng($im,$imageName);

        imagedestroy($im);

        return str_replace(FILE_UPLOAD_PATH,HTTPS_PATH,$imageName);
    }


    /**
     * @author chenniang
     * @DataTime: 2023-07-26 10:16
     * @功能说明:初始化没有画图的二维码
     */
    public function batchChannelQrImgInit($uniacid){

        $dis[] = ['uniacid','=',$uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['qr_img','=',''];

        $data = $this->where($dis)->order('id desc')->limit(20)->select()->toArray();

        if(!empty($data)){

            foreach ($data as $v){

                $qr = $this->channelQrImg($v);

                $this->dataUpdate(['id'=>$v['id']],['qr_img'=>$qr]);

            }
        }

        return true;
    }
















}